package sourceagile.server.doclet.java;

import java.io.File;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.FieldDoc;
import com.sun.javadoc.Tag;

/**
 * 
 * @Feature
 */
public class ConvertFileContentToJavaDocumentation {

	public static String CLASS_PACKAGE = "class_package";
	public static String CLASS_NAME = "class_name";
	public static String CLASS_DESCRIPTION = "class_description";
	public static String SUPER_CLASS = "super_class";
	public static String CLASS_MODIFIERS = "class_modifiers";
	public static String CLASS_IMPORTS = "class_imports";
	public static String CLASS_CONSTRUCTORS = "class_constructors";
	public static String CLASS_METHODS = "class_methods";
	public static String CLASS_FIELDS = "class_fields";
	public static String CLASS_TAGS = "class_tags";

	public static String FIELD_NAME = "field_name";
	public static String FIELD_TYPE = "field_type";

	public static JSONObject loadClassDocumentation(JSONObject jsonClassDoc,
			File file) {

		ClassDoc classDocumentation = GetJavaDocumentation.getClassDoc(file);

		jsonClassDoc = getObjectFromClassDoc(jsonClassDoc, classDocumentation);

		if (classDocumentation != null) {
			try {

				jsonClassDoc.put(CLASS_TAGS,
						listTags(classDocumentation.tags()));

				jsonClassDoc.put(CLASS_IMPORTS,
						listImports(classDocumentation.importedClasses()));

				jsonClassDoc.put(CLASS_CONSTRUCTORS, GetClassMethods
						.listConstructors(classDocumentation.constructors()));

				jsonClassDoc.put(CLASS_METHODS, GetClassMethods
						.listMethods(classDocumentation.methods()));

				jsonClassDoc.put(CLASS_FIELDS,
						listFields(classDocumentation.fields()));

			} catch (JSONException e) {

				e.printStackTrace();
			}
		}

		return jsonClassDoc;
	}

	private static JSONObject getObjectFromClassDoc(JSONObject jsonEntry,
			ClassDoc classDocumentation) {

		if (classDocumentation != null) {

			try {

				if (classDocumentation.containingPackage() != null) {

					jsonEntry.put(CLASS_PACKAGE, classDocumentation
							.containingPackage().name());
				}

				jsonEntry.put(CLASS_NAME, classDocumentation.name());

				jsonEntry.put(CLASS_DESCRIPTION,
						classDocumentation.commentText());

				if (classDocumentation.superclass() != null) {

					jsonEntry.put(SUPER_CLASS, classDocumentation.superclass()
							.toString());
				} else {

					jsonEntry.put(SUPER_CLASS, "");
				}

				jsonEntry.put(CLASS_MODIFIERS, classDocumentation.modifiers());

			} catch (JSONException e) {

				e.printStackTrace();
			}
		}

		return jsonEntry;
	}

	private static JSONArray listImports(ClassDoc[] imports) {

		JSONArray importsString = new JSONArray();

		for (int t = 0; t < imports.length; t++) {

			importsString.put(imports[t].toString());
		}

		return importsString;
	}

	private static JSONArray listFields(FieldDoc[] fieldDoc) {

		JSONArray jsonFields = new JSONArray();

		for (int i = 0; i < fieldDoc.length; i++) {

			try {

				JSONObject jsonEntry = new JSONObject();

				jsonEntry.put(FIELD_NAME, fieldDoc[i].name());

				jsonEntry.put(FIELD_TYPE, fieldDoc[i].type().toString());

				jsonFields.put(jsonEntry);

			} catch (JSONException e) {

				e.printStackTrace();
			}
		}

		return jsonFields;
	}

	private static JSONArray listTags(Tag[] tags) {

		JSONArray tagsString = new JSONArray();

		for (int t = 0; t < tags.length; t++) {

			tagsString.put(tags[t].name());
		}

		return tagsString;
	}

}
